package com.yxw.live_vod_backend_boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxw.live_vod_backend_boot.dto.PostDTO;
import com.yxw.live_vod_backend_boot.dto.PostDeleteDTO;
import com.yxw.live_vod_backend_boot.dto.PostQueryDTO;
import com.yxw.live_vod_backend_boot.entity.YxCsDeptEntity;
import com.yxw.live_vod_backend_boot.entity.YxCsPostEntity;
import com.yxw.live_vod_backend_boot.mapper.DeptMapper;
import com.yxw.live_vod_backend_boot.mapper.PostMapper;
import com.yxw.live_vod_backend_boot.service.IPostService;
import com.yxw.live_vod_backend_boot.util.PageUtils;
import com.yxw.live_vod_backend_boot.vo.PostVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 岗位管理Service业务层处理
 *
 * @author xiexuxin
 * @date 2023-09-17
 */
@Slf4j
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, YxCsPostEntity> implements IPostService {

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPost(PostDTO postDTO) {
        log.info("开始处理【添加岗位】的业务，参数：{}", postDTO);
        // 判断岗位名称是否已存在
        LambdaQueryWrapper<YxCsPostEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true, YxCsPostEntity::getPostName, postDTO.getPostName());
        Integer count = postMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BaseException("岗位名称【" + postDTO.getPostName() + "】已存在，请修改名称");
        }
        YxCsDeptEntity existingDept = deptMapper.selectById(postDTO.getDeptId());
        if (existingDept == null){
            throw new BaseException("未找到ID为【" + postDTO.getDeptId() + "】的所属部门");
        }
        // 创建岗位实体对象，并复制属性值
        YxCsPostEntity postEntity = new YxCsPostEntity();
        BeanUtil.copyProperties(postDTO, postEntity);
        //组装基本参数
        postEntity.setStatus(null == postEntity.getStatus() ? 1 : postEntity.getStatus());
        postEntity.setCreateTime(LocalDateTime.now());
        // 执行插入操作
        int rows = postMapper.insert(postEntity);
        // 判断插入操作是否成功
        if (rows != 1) {
            throw new BaseException("添加岗位失败，服务器忙，请稍后再尝试！");
        }
    }

    @Override
    public void deleteByPostIds(PostDeleteDTO dto) {
        log.info("开始处理【删除岗位】的业务，参数：{}", dto);
        // 判断待删除的岗位列表是否为空
        if (dto == null || dto.getPostIds().isEmpty()) {
            throw new BaseException("删除岗位失败，尝试删除的岗位数据不存在！");
        }
        // 执行批量删除操作
        int rows = postMapper.deleteByPostIds(dto.getPostIds());
        // 执行批量删除操作
        if (rows != dto.getPostIds().size()) {
            throw new BaseException("删除岗位失败，服务器忙，请稍后再试！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePost(PostDTO postDTO) {
        log.info("开始处理【修改岗位】的业务，参数：{}", postDTO);
        // 查询待修改的岗位是否存在
        PostVO queryResult = postMapper.selectPostById(postDTO.getPostId());
        // 判断岗位是否存在
        if (queryResult == null || queryResult.getPostId() == 0) {
            throw new BaseException("修改岗位失败，尝试修改的岗位数据不存在！");
        }
        // 判断岗位名称是否被占用
        LambdaQueryWrapper<YxCsPostEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(true, YxCsPostEntity::getPostName, postDTO.getPostName());
        Integer count = postMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BaseException("修改岗位失败，岗位名称已经被占用！");
        }
        YxCsDeptEntity existingDept = deptMapper.selectById(postDTO.getDeptId());
        if (existingDept == null){
            throw new BaseException("未找到ID为【" + postDTO.getDeptId() + "】的所属部门");
        }
        // 创建岗位实体对象，并复制属性值
        YxCsPostEntity postEntity = new YxCsPostEntity();
        BeanUtils.copyProperties(postDTO, postEntity);
        postEntity.setPostId(postDTO.getPostId());
        postEntity.setUpdateTime(LocalDateTime.now());
        // 执行更新操作
        int rows = postMapper.updateById(postEntity);
        // 判断更新操作是否成功
        if (rows != 1) {
            throw new BaseException("修改岗位失败，服务器忙，请稍后再试！");
        }
    }

    @Override
    public PostVO selectPostById(Long postId) {
        log.info("开始处理【根据岗位id查询】的业务，参数：{}", postId);
        return postMapper.selectPostById(postId);
    }

    @Override
    public List<PostVO> getPostByDept(Long deptId) {
        log.info("开始处理【根据部门查询岗位】的业务，参数：{}", deptId);
        return postMapper.selectPostByDept(deptId);
    }

    @Override
    public PageVO<PostVO> getPostList(PostQueryDTO postQueryDTO) {
        log.info("开始处理【条件查询岗位列表】的业务，条件参数：{}", postQueryDTO);
        Page<PostVO> page = new Page<>(postQueryDTO.getCurrPage(),postQueryDTO.getPageSize());
        List<PostVO> list = baseMapper.getPostByCondition(page,postQueryDTO);
        return PageUtils.get(page, list);
    }

    @Override
    public List<PostVO> postList() {
        log.info("开始处理【查询岗位列表】的业务，无参数");
        return postMapper.postList();
    }

}
